package com.binary.coder.gift.core.interfaces.Invitation.facade;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.binary.coder.gift.core.application.Invitation.InvitationApplication;
import com.binary.coder.gift.core.common.bean.PageBean;
import com.binary.coder.gift.core.common.bean.ResultBean;
import com.binary.coder.gift.core.domain.invitation.model.aggregate.HandleInvitationRecordAggregate;
import com.binary.coder.gift.core.domain.invitation.model.aggregate.InvitationCardAggregate;
import com.binary.coder.gift.core.domain.invitation.model.entity.InvitationEntrustEntity;
import com.binary.coder.gift.core.domain.invitation.model.entity.InvitationRecordEntity;
import com.binary.coder.gift.core.interfaces.Invitation.assembler.InvitationCardAssembler;
import com.binary.coder.gift.core.interfaces.Invitation.dto.req.*;
import com.binary.coder.gift.core.interfaces.Invitation.dto.res.QueryHandleInvitationRecordRes;
import com.binary.coder.gift.core.interfaces.Invitation.dto.res.QueryInvitationCardRes;
import com.binary.coder.gift.core.interfaces.Invitation.dto.res.QueryInvitationEntrustRes;
import com.binary.coder.gift.core.interfaces.Invitation.dto.res.UpcomingEventRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author atao
 * @version 1.0.0
 * @ClassName InvitationInterfacs.java
 * @Description 请帖接口
 * @createTime 2024年06月21日 16:58:00
 */
@Slf4j
@Api(tags = "请帖相关接口")
@RestController
@RequestMapping("/api/invitation")
public class InvitationController {

    @Resource
    private InvitationApplication invitationApplication;

    @ApiOperation("请帖-分页")
    @GetMapping("/page/query/create")
    public ResultBean<PageBean<QueryInvitationCardRes>> pageQueryInvitationCard(PageQueryInvitationCardReq req) {
        PageBean<InvitationCardAggregate> res = invitationApplication.pageQueryInvitationCard(req);
        return ResultBean.success(InvitationCardAssembler.assemblerQueryInvitationCardRes(res));
    }


    @ApiOperation("app-请帖-保存")
    @PostMapping("/app/save")
    public ResultBean saveInvitationCardFromApp(@RequestBody SaveAppInvitationCardReq req) {
        log.info("{}",req);
        invitationApplication.saveAppInvitationCard(InvitationCardAssembler.assemblerAppInvitationCardEntity(req,StpUtil.getTokenInfo().getLoginId()));
        return ResultBean.success();
    }

    @ApiOperation("请帖-保存")
    @PostMapping("/web/save")
    public ResultBean saveInvitationCard(@RequestBody SaveInvitationCardReq req) {
        invitationApplication.saveInvitationCard(InvitationCardAssembler.assemblerInvitationCardAggregate(req));
        return ResultBean.success();
    }

    @ApiOperation("请帖-删除")
    @DeleteMapping("/web/delete/{invitationCardId}/{userId}")
    public ResultBean deleteInvitationCardById(@PathVariable("invitationCardId") Long invitationCardId,@PathVariable("userId") Long userId) {
        invitationApplication.deleteInvitationCardById(userId,invitationCardId);
        return ResultBean.success();
    }

    @ApiOperation("处理请帖邀请-保存")
    @PostMapping("/handle/save")
    public ResultBean handleInvitationCard(@RequestBody HandleInvitationReq req) {
        invitationApplication.handleInvitationCard(BeanUtil.copyProperties(req, InvitationRecordEntity.class));
        return ResultBean.success();
    }

    @ApiOperation("处理请帖邀请-查看")
    @PostMapping("/handle/query/{invitationCardId}/{userId}")
    public ResultBean<QueryHandleInvitationRecordRes> queryInvitationRecord(@PathVariable("invitationCardId") Long invitationCardId, @PathVariable("userId") Long userId) {
        HandleInvitationRecordAggregate invitationRecordAggregate = invitationApplication.queryInvitationRecord(userId, invitationCardId);
        return ResultBean.success(InvitationCardAssembler.assemblerQueryHandleInvitationRecordRes(invitationRecordAggregate));
    }

    @ApiOperation("处理请帖邀请-分页")
    @PostMapping("/handle/query/page")
    public ResultBean<PageBean<QueryHandleInvitationRecordRes>> pageQueryInvitationRecord(PageQueryInvitationRecordReq req) {
        PageBean<HandleInvitationRecordAggregate> res = invitationApplication.pageQueryInvitationRecord(req);
        return ResultBean.success(InvitationCardAssembler.assemblerQueryHandleInvitationRecordRes(res));
    }

    @ApiOperation("请帖委托-新增")
    @PostMapping("/entrust/save")
    public ResultBean saveInvitationEntrust(SaveInvitationEntrustReq req) {
        invitationApplication.saveInvitationEntrust(BeanUtil.copyProperties(req, InvitationEntrustEntity.class));
        return ResultBean.success();
    }

    @ApiOperation("请帖委托-分页")
    @PostMapping("/entrust/query/page")
    public ResultBean<PageBean<QueryInvitationEntrustRes>> pageQueryInvitationEntrust(PageQueryInvitationEntrustReq req) {
        return ResultBean.success(invitationApplication.pageQueryInvitationEntrust(req));
    }

    @ApiOperation("请帖委托-状态处理")
    @PostMapping("/entrust/handle")
    public ResultBean handleInvitationEntrust(PageQueryInvitationEntrustReq req) {
        return ResultBean.success(invitationApplication.pageQueryInvitationEntrust(req));
    }

    @GetMapping("/upcoming-events")
    public ResultBean<List<UpcomingEventRes>> getUpcomingEvents() {

        log.warn("loginId:{}", StpUtil.getTokenInfo());

        List<UpcomingEventRes> events = new ArrayList<>();


        events.add(UpcomingEventRes.builder()
                .content("距离张三与李四的婚宴")
                .tag("婚宴")
                .daysLeft(15)
                .build()
        );

        events.add(UpcomingEventRes.builder()
                .content("距离王五的生日宴会")
                .tag("生日")
                .daysLeft(3)
                .build()
        );
        return ResultBean.success(events);
    }

}
